package com.example.demo.controller;

import com.example.demo.service.UserService;
import com.example.demo.service.SiteSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ChatController {
    
    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);
    private final UserService userService;
    private final SiteSettingsService siteSettingsService;

    @Autowired
    public ChatController(UserService userService, SiteSettingsService siteSettingsService) {
        this.userService = userService;
        this.siteSettingsService = siteSettingsService;
    }

    @GetMapping("/chat-room")
    public String chatRoom(Model model) {
        // 检查用户是否已登录
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() && 
            !authentication.getPrincipal().equals("anonymousUser")) {
            
            model.addAttribute("user", userService.getCurrentUser());
            model.addAttribute("isAuthenticated", true);
            logger.info("User is logged in, accessing chat room");
        } else {
            model.addAttribute("isAuthenticated", false);
            logger.info("Anonymous user accessing chat room");
        }
        
        // 获取聊天室背景设置
        String chatRoomBackground = siteSettingsService.getSettingValue("chatroom.background", "");
        String chatRoomBackgroundOpacity = siteSettingsService.getSettingValue("chatroom.background.opacity", "0.5");
        
        // 添加到模型中
        model.addAttribute("chatRoomBackground", chatRoomBackground);
        model.addAttribute("chatRoomBackgroundOpacity", chatRoomBackgroundOpacity);
        
        return "chat-room";
    }
} 